package com.mz.ozy.activity.find;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mz.ozy.R;
import com.mz.ozy.WpkApp;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.ArticleDetailBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.mvp.presenter.StrategyDetailPresenter;
import com.mz.ozy.mvp.view.StrategyDetailContract;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.MyWebView;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import cn.xuzhijun.proportional.ProportionalFrameLayout;

public class StrategyDetailActivity extends BaseActivity<StrategyDetailContract.View, StrategyDetailPresenter> implements StrategyDetailContract.View {

    public static final String ARTICLE_ID = "article_id";
    private Unbinder unbinder;
    @BindView(R.id.tv_strategy_title)
    TextView mTvStrategyTitle;
    @BindView(R.id.tv_strategy_typename)
    TextView mTvStrategyType;
    @BindView(R.id.tv_strategy_time)
    TextView mTvStrategyTime;
    @BindView(R.id.tv_strategy_readnum)
    TextView mTvStrategyReadnum;
    @BindView(R.id.tv_strategy_likenum)
    TextView mTvStrategyLikenum;
    @BindView(R.id.ll_strategy_likenum)
    LinearLayout mLlStrategyLikenum;
    @BindView(R.id.iv_strategy_likenum)
    ImageView mIvStrategyLikenum;
    @BindView(R.id.wv_strategy_content)
    WebView mWvStrategyContent;

    @BindView(R.id.vv_course_train_video)
    JzvdStd mVvCourseTrainVideo;
    //    @BindView(R.id.vv_idyllic_detail)
//    ProportionalFrameLayout mVideoIdyllic;
    @BindView(R.id.vv_idyllic_detail)
    LinearLayout mVideoIdyllic;
    private int wid;

    private boolean isAttentioning = false;
    private boolean isAttention = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_strategy_detail);
        unbinder = ButterKnife.bind(this);
        initTitle();
        tvTitle.setText("详情");
        initData();
        initEvents();
        rightImage.setVisibility(View.VISIBLE);
        rightImage.setImageResource(R.mipmap.icon_collect_star);
        requestDataForNetwork();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.resetAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
            unbinder = null;
        }
    }

    private void initEvents() {
        mLlStrategyLikenum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isAttentioning) {
                    isAttentioning = true;
                    mPresenter.followTheObject(StrategyDetailActivity.this, wid, 1);
                }
            }
        });
        rightImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isAttentioning) {
                    isAttentioning = true;
                    mPresenter.followTheObject(StrategyDetailActivity.this, wid, 1);
                }
            }
        });
    }

    private void initData() {
        wid = getIntent().getIntExtra(ARTICLE_ID, 0);
    }

    private void requestDataForNetwork() {
        mPresenter.queryArticleDetail(this, wid);
    }

    @Override
    public void getArticleDetail(ArticleDetailBean listBean) {
        setAttentionState(1 == listBean.getIsCollection());
        mTvStrategyLikenum.setText(listBean.getCollectionNum() + "");
        ArticleDetailBean.ArticleDetail map = listBean.getHuatMap();
        if (map != null) {
            mTvStrategyTitle.setText(map.getTitle());
            mTvStrategyTime.setText(map.getCreated());
            mTvStrategyReadnum.setText(map.getBrowseNum() + "");
            mTvStrategyType.setText(map.getTypeName());
            if (map.getSctype() == 1) {
                if (!TextUtils.isEmpty(map.getVido())) {
                    mVideoIdyllic.setVisibility(View.VISIBLE);
                    String proxyUrl = WpkApp.getProxy(this).getProxyUrl(map.getVido());
                    mVvCourseTrainVideo.setUp(proxyUrl, "");
                    Glide.with(this).load(map.getImg1()).into(mVvCourseTrainVideo.thumbImageView);
                }
            } else {
                mVideoIdyllic.setVisibility(View.GONE);
            }
//            mWvStrategyContent.loadData(map.getContent());
            mWvStrategyContent.loadDataWithBaseURL(null, setHtmlContent(Utils.getNewContent(map.getContent())), "text/html", "utf-8", null);
        }
    }

    public String setHtmlContent(String contents) {
        String head = ("<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:auto; height:auto;} body{word-break:break-all;background-color:#ffffff;color:#000000;font-size:12px}</style>" +
                "</head>");
        return "<html>" + head + "<body style=\"word-wrap:break-word;\">" + contents + "</body>" + "</html>";
    }

    @Override
    public void handleFailResponse(NetBean netBean) {
        super.handleFailResponse(netBean);
        isAttentioning = false;
    }

    @Override
    public void followSuccess(JSONObject jsonObject) {
//        ToastUtils.showToast("收藏成功");
        setAttentionState(!isAttention);
        isAttentioning = false;
    }
    private void setAttentionState(boolean attention) {
        if (attention) {
            rightImage.setImageResource(R.mipmap.icon_collect_star_yes);
            this.isAttention = attention;
        } else {
            rightImage.setImageResource(R.mipmap.icon_collect_star);
            this.isAttention = attention;
        }
    }
}
